const db = wx.cloud.database();
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputVal: "", // 搜索框值
    catList: [], //搜索渲染推荐数据
    act: 1,
    index: 0,
    index2: 0,
    index3: 0,
    index4: 0,
    index5: 0,
    nianfen: ['请选择年份'],
    gaoxiao: ['请选择高校'],
    yuanxi: ['请选择院系'],
    zydm: ['请选择专业代码'],
    yjfx: ['请选择专业名称及研究方向'],
    phone: 0,
    theList: ''
  },
  onLoad: function (options) {
    var _ = this;
    var phone = wx.getStorageSync('zyjsphone')
    console.log(phone)
    if(phone){
      _.setData({
        phone:true
      })
    }
    console.log(options)
    _.setData({
      area:options.area,
      uid:options.agent,
      page:options.page
    })
    var phone = wx.getStorageSync('phone')
    if(phone){
      _.setData({
        phone:!0
      })
    }
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=45812&callback=?', //接收url
      data: {
        level: '1',
        nianfen: ''
      }, //接收参数
      success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        var list = [];
        list.push('请选择年份')
        for (var i in theJson.lists) { //循环写入下来框

          list.push(theJson.lists[i].nianfen)
        }
        _.setData({
          nianfen: list
        });
      }
    })

    
  },
 
  bindPickerChange: function (e) {
    let num = e.detail.value;
    let _ = this;
    console.log('nianfen发送选择改变，携带值为', _.data.nianfen[num]);
    this.setData({
      index: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=45812&callback=?', //接收url
      data: {
        level: '2',
        grfiled: 'nianfen',
        grtext: _.data.nianfen[num]
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson)
        var list = [];
        list.push('请选择高校')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].gaoxiao)
        }
        _.setData({
          gaoxiao: list
        });
      
      }
    })
  },
  bindPickerChange2: function (e) {
    let num2 = e.detail.value;
    let _ = this;
    console.log('gaoxiao发送选择改变，携带值为', _.data.gaoxiao[num2]);
    this.setData({
      index2: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=45812&callback=?', //接收url
      data: {
        level: '3',
        grfiled: 'gaoxiao',
        grtext: _.data.gaoxiao[num2],
        onefiled: 'nianfen',
        onetext: _.data.nianfen[this.data.index]
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson)
        var list = [];
        list.push('请选择院系')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].yuanxi)

        }
        _.setData({
          yuanxi: list
        });
      }
    })
  },
  bindPickerChange3: function (e) {
    let num3 = e.detail.value;
    let _ = this;
    console.log('yuanxi发送选择改变，携带值为', _.data.yuanxi[num3]);
    this.setData({
      index3: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=45812&callback=?', //接收url
      data: {
        level: '4',
        grfiled: 'yuanxi',
        grtext: _.data.yuanxi[num3],
        onefiled: 'nianfen',
        onetext: _.data.nianfen[this.data.index],
        twofiled: 'gaoxiao',
        twotext: _.data.gaoxiao[this.data.index2],
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson.lists)
        var list = [];
        list.push('请选择专业代码')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].zydm)
        }
        _.setData({
          zydm: list
        });
      }
    })
  },
  bindPickerChange4: function (e) {
    let num4 = e.detail.value;
    let _ = this;
    console.log('zydm发送选择改变，携带值为', _.data.zydm[num4]);
    this.setData({
      index4: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=45812&callback=?', //接收url
      data: {
        level: '5',
        grfiled: 'zydm',
        grtext: _.data.zydm[num4],
        onefiled: 'nianfen',
        onetext: _.data.nianfen[this.data.index],
        twofiled: 'gaoxiao',
        twotext: _.data.gaoxiao[this.data.index2],
        threefiled: 'yuanxi',
        threetext: _.data.yuanxi[this.data.index3],
      }, //接收参数
      success(res) {
        console.log(res)
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson)
        console.log(theJson.lists)
        var list = [];
        list.push('请选择专业名称及研究方向')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].yjfx)
        }
        _.setData({
          yjfx: list
        });
      }
    })

  },
  bindPickerChange5: function (e) {
    let num5 = e.detail.value;
    let _ = this;
    console.log('yjfx', _.data.yjfx[num5]);
    this.setData({
      index5: e.detail.value
    })


  },

  tijiao: function (res) {
    var _ = this;
    var nianfen = _.data.nianfen[_.data.index];
    var gaoxiao = _.data.gaoxiao[_.data.index2];
    var yuanxi = _.data.yuanxi[_.data.index3];
    var zydm = _.data.zydm[_.data.index4];
    var yjfx = _.data.yjfx[_.data.index5];
    wx.request({ //发送请求
      url: "https://zg99.offcn.com/index/chaxun/writelogs/?actid=45812&callback=?",
      data: {
        phone: wx.getStorageSync('zyjsphone'),
        gaoxiao:gaoxiao,
        yuanxi:yuanxi,
        zydm:zydm
      }, //接收参数
      success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        };
        console.log(theJson)
      }
    })
    wx.request({ //发送请求
      url: "https://zg99.offcn.com/index/chaxun/getlist/?actid=45812&callback=?",
      data: {
        nianfen: nianfen,
        gaoxiao: gaoxiao,
        yuanxi: yuanxi,
        zydm: zydm,
        yjfx:yjfx,
        limits: '100'
      }, //接收参数
      success(res) { //返回
        
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        };
        console.log(theJson.lists)
        if (theJson.status == '2') {
          console.log('暂无数据！')
          wx.showToast({
            title: '暂无数据',
            icon: 'none',
            duration: 2000
          })
        } else {
         _.setData({
          theList:theJson.lists
         })
         wx.pageScrollTo({
           duration: 1000,
           scrollTop:500
         })
      
        }

      }
    })
  },
  tijiao1: function (res) {
    var _ = this;
    var nianfen1 = _.data.province[_.data.index1];
    var xianqu1 = _.data.city[_.data.index12];
    var zkz = _.data.zkz;
    wx.request({ //发送请求
      url: "https://zg99.offcn.com/index/chaxun/updateuser/?actid=45812&callback=?",
      data: {
        phone: wx.getStorageSync('fscxphone'),
        zkz: zkz
      }, //接收参数
      success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        };
        console.log(theJson)
      }
    })
    wx.request({ //发送请求
      url: "https://zg99.offcn.com/index/chaxun/getlist/?actid=45812&callback=?",
      data: {
        nianfenyi: nianfen1,
        xianquyi: xianqu1,
        tabnum: 2,
        limits: '100'
      }, //接收参数
      success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        };
        console.log(theJson)
        if (theJson.status == '2') {
          console.log('暂无数据！')
          wx.showToast({
            title: '暂无数据',
            icon: 'none',
            duration: 2000
          })
        } else {
          wx.setStorageSync('theList1',  theJson.lists[0])
          wx.navigateTo({
            url: '../result/result?List=1',
          })
        }

        console.log(_.data.theList)
      }
    })
  },

  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})